package com.crr.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class DeadLetterConfig {
    /**
     * 死信交换器
     * @return
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("dead-ex");
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue deadQueue(){
        return new Queue("dead-queue");
    }

    /**
     * 死信队列绑定到死信交换机上
     * @param directExchange
     * @param deadQueue
     * @return
     */
    @Bean
    public Binding deadBind(DirectExchange directExchange ,Queue deadQueue){
        return BindingBuilder.bind(deadQueue).to(directExchange).with("deadKey");
    }

    /**
     * 正常交换器
     * @return
     */
    @Bean
    public DirectExchange normalExchange(){

        return new DirectExchange("normal-ex");
    }

    /**
     * 正常队列
     * @return
     */
    @Bean
    public Queue normalQueue(){
        String queueName="normal-queue";
        Map<String,Object> arg=new HashMap<>();
        //死信交换器
        arg.put("x-dead-letter-exchange","dead-ex");
        //死信交换器路由键
        arg.put("x-dead-letter-routing-key","deadKey");
        //队列的ttl
        arg.put("x-message-ttl",5000);
        return new Queue(queueName,true,false,false,arg);
    }

    /**
     * 正常队列绑定到正常交换器上并指明路由键
     * @param normalExchange
     * @param normalQueue
     * @return
     */
    @Bean
    Binding normalBind(DirectExchange normalExchange ,Queue normalQueue){
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("order");

    }
}
